package com.jc.demo.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@SuppressWarnings("Duplicates")
public class QueryVersionsDataDemo {

    public static void main(String[] args) throws IOException {
        // Instantiating Configuration class
        Configuration config = HBaseConfiguration.create();

        // Instantiating HTable class
        Connection connection = ConnectionFactory.createConnection(config);
        Table hTable = connection.getTable(TableName.valueOf("emp"));
        try {


            // Instantiating Get class
            Get g = new Get(Bytes.toBytes("row1"));

            g.readVersions(3);  // will return last 3 versions of row
            // Reading the data
            Result result = hTable.get(g);


            List<Cell> cells = result.listCells();

            if (null != cells && !cells.isEmpty()) {
                for (Cell ce : cells) {
                    String family = Bytes.toString(ce.getFamilyArray(), ce.getFamilyOffset(), ce.getFamilyLength());
                    String qualifier = Bytes.toString(ce.getQualifierArray(), ce.getQualifierOffset(), ce.getQualifierLength());
                    String value = Bytes.toString(ce.getValueArray(),
                            ce.getValueOffset(),
                            ce.getValueLength());
                    System.out.println("family:" + family + ", qualifier=" + qualifier + ", value=" + value + ",timestamp:" + ce.getTimestamp());
                }
            }



        } finally {
            hTable.close();
            connection.close();
        }

    }
}
